import 'package:flutter/material.dart';
import 'package:flutter_nativedialog_plugin/loadingdialog_plugin.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            getButton('显示1个弹窗(返回按钮可以关闭dialog)', () {
              LoadingDialogPlugin.showHttpLoading("1", canCancel: true);
            }),
            getButton('显示2个弹窗(返回按钮可以关闭dialog)', () async {
              LoadingDialogPlugin.showHttpLoading("3", canCancel: true);
              LoadingDialogPlugin.showHttpLoading("2", canCancel: true);
              LoadingDialogPlugin.showHttpLoading("2", canCancel: true);
            }),
            getButton('显示1个弹窗(返回按钮可以关闭dialog),有文本提示', () {
              LoadingDialogPlugin.showHttpLoading("4", msg: "加载中", canCancel: true, onCancelListener: (tag) {
                print("物理返回按钮关闭了dialog,tag:$tag");
              });
            }),
            getButton('显示弹窗(返回按钮可以关闭dialog),3秒后自动调用代码关闭dialog', () {
              LoadingDialogPlugin.showHttpLoading("5", canCancel: true);
              Future.delayed(Duration(seconds: 3), () {
                print("代码关闭dialog");
                LoadingDialogPlugin.hideHttpLoading("5");
              });
            }),
            getButton('显示用于加载框(能手动关闭)', () {
              LoadingDialogPlugin.showLoading(canCancel: true);
            }),
            getButton('显示用于加载框(不能手动关闭)', () {
              LoadingDialogPlugin.showLoading(canCancel: false);
            }),
          ],
        ),
      ),
    );
  }

  ///获取通用演示按钮
  static Widget getButton(String text, VoidCallback onPressed) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
      child: RaisedButton(
        textColor: Colors.white,
        color: Colors.blue,
        child: Padding(
          padding: const EdgeInsets.symmetric(vertical: 12),
          child: Text(
            text,
            textAlign: TextAlign.center,
          ),
        ),
        onPressed: onPressed,
      ),
    );
  }
}
